<?php namespace App\Utils;


class ChuangLanVoiceCode {

    public function createUrl($funAndOperate) {
        $BASE_URL = "https://voice.253.com/20141029/accounts/";
        $ACCOUNT_SID = "cbde49fd58f84a3eac42ae7da3e91278"; // 主账户SID

        $AUTH_TOKEN = "ff948ce067954754805518695a79fbba"; // 主账号TOKEN
        // 时间戳
        date_default_timezone_set("Asia/Shanghai");
        $timestamp = date("YmdHis");

        // 签名
        $sig = md5($ACCOUNT_SID.$AUTH_TOKEN.$timestamp);

        return $BASE_URL . $ACCOUNT_SID . "/" . $funAndOperate . "sig=" . $sig . "&timestamp=" . $timestamp;
    }

    public function createHeaders($body) {
        $CONTENT_TYPE = "application/json";
        $ACCEPT = "application/json";
        $headers = array('Content-type: '.$CONTENT_TYPE, 'Accept: '.$ACCEPT, 'Content-Length: '.strlen($body));
        return $headers;
    }

    public function post($funAndOperate, $body) {

        // 构造请求数据
        $url = self::createUrl($funAndOperate);
        $headers = self::createHeaders($body);

//        echo("url:<br/>" . $url);
//        echo("<br/><br/>body:<br/>" . $body);
//        echo("<br/><br/>headers:<br/>");
//        var_dump($headers);

        // 提交请求
        $con = curl_init();
        curl_setopt($con, CURLOPT_URL, $url);
        curl_setopt($con, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($con, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt ($con, CURLOPT_HEADER, 0);
        curl_setopt($con, CURLOPT_POST, 1);
        curl_setopt ($con, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($con, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($con, CURLOPT_POSTFIELDS, $body);
        $result = curl_exec($con);
        curl_close($con);

        return "".$result;
    }
}
?>